package com.junci.mtest.utils;

import android.os.AsyncTask;
import android.util.Log;


import com.junci.mtest.model.Constants;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;


public class LogUtil {

    private static boolean isLog = true;
//    private static boolean isLog = false;// TODO 正式包打包需要改的




    public static boolean isLog() {
        return isLog;
    }

    public static void setLog(boolean log) {
        isLog = log;
    }

    /**
     * 将一些报错的错误日志，全部记录下来，下次传到后台分析
     *
     * @param msg
     */
    public static void writeErrorToFile(final String msg) {
        AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                    try {
                    String day = DateUtil.getDay(System.currentTimeMillis());
                    File file = new File(Constants.FILE_BASE + "/error_log_" + day + ".txt"); // 有些手机不能直接打开log，为了方便调试，后缀更改为txt
                    FileUtil.initPath();
                    String log = "\n" + DateUtil.currentDatetime() + "  " + System.currentTimeMillis() + " :\n" + msg + "\n\n\n";   //log日志内容，可以自行定制
                    //                if (!file.exists()) {
                    //                    file.mkdirs();//创建父路径
                    //}
                    FileOutputStream fos = null;//FileOutputStream会自动调用底层的close()方法，不用关闭
                    BufferedWriter bw = null;
                    try {
                        fos = new FileOutputStream(file, true);//这里的第二个参数代表追加还是覆盖，true为追加，flase为覆盖
                        bw = new BufferedWriter(new OutputStreamWriter(fos));
                        bw.write(log);
                    } catch (Exception e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            if (bw != null) {
                                bw.close();//关闭缓冲流
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }


    /**
     * 将一些报错的错误日志，全部记录下来，下次传到后台分析
     *
     * @param ex
     */
    public static void writeErrorToFile(Throwable ex) {

        // 将报错日志直接搞到服务器去
        StringBuilder ss = new StringBuilder();
        try {
            ss.append("have exception：\n").append(ex.toString()).append("  \n");
            for (Throwable throwable : ex.getSuppressed()) {
                for (StackTraceElement stackTraceElement : throwable.getStackTrace()) {
                    ss.append(stackTraceElement.toString()).append("\n");
                }
            }
            for (StackTraceElement stackTraceElement : ex.getCause().getStackTrace()) {
                // 最主要的是这里，这里会打印报错的行数
                ss.append(stackTraceElement.toString()).append("\n");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        final String msg = ss.toString();
        writeErrorToFile(msg);
    }



    public static void s(String s) {
        if (isLog) {
            s("Mtest：", s);
        }
    }

    public static void s(String TAG, String s) {
        if (isLog) {
            System.out.println(TAG + " " + s);
        }
    }

    public static void e(String s) {
        if (isLog) {
            System.err.println("Mtest： " + s);
        }
    }

    public static void e(Exception e) {
        if (isLog) {
            if (e != null) {
                System.err.println("Mtest：  已捕获异常：" + e.getMessage() + "\n" + e.toString());
            }
        }
    }

    public static void d(String TAG, String debug) {
        if (isLog) {
            Log.d(TAG, debug);
        }
    }

    public static void i(String TAG, String info) {
        if (isLog) {
            Log.i(TAG, info);
        }
    }

    public static void w(String TAG, String warn) {
        if (isLog) {
            Log.w(TAG, warn);
        }
    }

    public static void v(String TAG, String verbose) {
        if (isLog) {
            Log.v(TAG, verbose);
        }
    }

    public static void E(String TAG, String clzName, Throwable throwable) {
        if (isLog) {
            Log.e(TAG, clzName + " / " + throwable.getMessage(), throwable);
        }
    }
}

